﻿using Hotel.Application.Suppliers.Dto;
using Hotel.Domain;
using Hotel.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.Application.Suppliers
{
    public class SupplierService : BaseService<Supplier, SupplierCreateUpdateDto, SupplierListDto>, ISupplierService
    {
        private readonly IRepository<Supplier> repository;

        public SupplierService(IRepository<Supplier> repository) : base(repository)
        {
            this.repository = repository;
        }

        

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ResultDto> CreateAsync(SupplierCreateUpdateDto dto)
        {
            await cache.GetOrAddAsync("memlist", async () => {
                return mapper.Map<List<SupplierListDto>>(await repository.QueryAsync());
            });


            var entity = await repository.FindAsync(m => m.SupplierCode == dto.SupplierCode);
            if (entity != null)
            {
                return new ResultDto
                {
                    code = State.Failed,
                    msg = "供应商编码重复"
                };
            }

            await repository.InsertAsync(mapper.Map<SupplierCreateUpdateDto, Supplier>(dto));
            return new ResultDto
            {
                code = State.Success
            };
        }

        public async Task MockDataAsync()
        {
            string[] Province = "河北、山西、辽宁、吉林、黑龙江、江苏、浙江、安徽、福建、江西、山东、河南、湖北、湖南、广东、海南、四川、贵州、云南、陕西、甘肃、青海、台湾".Split('、');

            for (int i = 0; i < 1000; i++)
            {
                Random random = new Random();
                SupplierCreateUpdateDto dto = new SupplierCreateUpdateDto 
                {
                    Address = Province[random.Next(0, Province.Length - 1)],
                    Contact = "张三",
                    Email = "zhagnsan@qq.com",
                    Fax = "010-999983234",
                    Phone = "13812231231",
                    SupplierCode = Guid.NewGuid().ToString("n"),
                    SupplierName = $"数码供应商_{random.Next(1000, 9999)}",
                    SupplierType = Domain.Enum.SupplierType.@virtual
                };

                await repository.InsertAsync(mapper.Map<SupplierCreateUpdateDto,Supplier>(dto));
            }
        }
    }
}
